package switchviz.graphic.gauge;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

import javax.media.opengl.GL;
import javax.media.opengl.GLAutoDrawable;

import switchviz.graphic.GraphicEventListener;
import switchviz.graphic.GraphicUtils;

public class DetailGaugeEventListener extends GraphicEventListener implements ActionListener
{
	private IGauge _gauge = null;
	private GaugeDetailed _detailed = null;
	private GaugeDialog _gaugedialog = null;
	
	public DetailGaugeEventListener(IGauge gauge, GaugeDetailed detailed)
	{
		_gauge = gauge;
		_detailed = detailed;
		_gauge.setDetailLevel(1);
	}
	
	public void display(GLAutoDrawable drawable)
	{
		super.display(drawable);
		if (_gauge == null) return;
		
		GL gl = drawable.getGL();
		
		gl.glPushMatrix();
		
		gl.glScalef(0.7f, 0.7f, 0.7f);
		
		// print name
		gl.glTranslatef(-0.05f, -1.0f, 0.0f);
		gl.glColor3f(1.0f, 1.0f, 1.0f);
		
		if(_gauge.getGaugeDataModel().isMultiplePerPort())
			GraphicUtils.drawString(gl, 8, GraphicUtils.ALIGN_RIGHT, 0, _gauge.getGaugeDataModel().getName() + " @ " + _gauge.getGaugeDataModel().getPort());
		else
			GraphicUtils.drawString(gl, 8, GraphicUtils.ALIGN_RIGHT, 0, _gauge.getGaugeDataModel().getName());
		
		
		gl.glTranslatef(0.2f, 0.0f, 0.0f);
		_detailed.draw(gl);
		gl.glTranslatef(-0.15f, 1.3f, 0.0f);
		
		gl.glScalef(0.6f, 0.6f, 0.6f);
		rotateEachCell(gl);
		
		_gauge.draw(gl);
		gl.glPopMatrix();
	}
	
	public void init(GLAutoDrawable drawable)
	{
		super.init(drawable);
		if(_gauge == null) return;
		_gauge.initialize(drawable.getGL());
	}
	
	public void mousePressed(MouseEvent evt) 
	{
		super.mousePressed(evt);

		if(evt.getButton() == MouseEvent.BUTTON3)
		{
			_gaugedialog = GaugeDialog.getInstance();
			
			_gaugedialog.setGauge(_gauge);

			_gaugedialog.addActionListener(this);
		}
	}

	public void actionPerformed(ActionEvent evt) 
	{
		String command = evt.getActionCommand();
		if(command.equals(GaugeDialog.APPLY))
		{
			_gauge.setElement(_gaugedialog.getElement());
		}
		_gaugedialog.setVisible(false);
	}
}
